package com.backedserver.controller.judicialpersonal;

import com.common.base.BindCreateBy;
import com.common.base.BindUpdateBy;
import com.common.constants.Constants;
import com.common.entity.system.DictionaryEntity;
import com.common.global.BizException;
import com.common.global.BizServiceEnum;
import com.common.repository.system.DictionaryRepository;
import com.common.requestvo.judicialcheck.JudicialTargetTypeSumCheckJobExplainReqVo;
import com.common.requestvo.judicialpersonal.BusinessJobExperienceReqVo;
import com.common.requestvo.judicialpersonal.JobChangeExperienceReqVo;
import com.common.requestvo.judicialpersonal.JobExplainReqVo;
import com.common.requestvo.judicialpersonal.JudicialPersonalReqVo;
import com.common.service.casemanage.CaseService;
import com.common.service.judicialcheck.JudicialTargetTypeSumCheckService;
import com.common.service.judicialpersonal.BusinessJobExperienceService;
import com.common.service.judicialpersonal.JobChangeExperienceService;
import com.common.service.judicialpersonal.JobExplainService;
import com.common.service.judicialpersonal.JudicialPersonalService;
import com.common.utils.UploadUtils;
import com.common.utils.PageUtil;
import com.common.utils.ResultVoUtil;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.judicialcheck.JudicialPersonalFileVo;
import com.common.vo.judicialcheck.JudicialTargetTypeSumCheckVo;
import com.common.vo.judicialpersonal.JudicialPersonalDetailVo;
import com.common.vo.judicialpersonal.JudicialPersonalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 司法人员管理 Controller
 *
 * @author johnny
 * @create 2021-05-13 4:27 下午
 **/
@RestController
@RequestMapping("/judicial_personal")
@Api(tags = "司法人员管理模块")
public class JudicialPersonalManagementController {

    private final JudicialPersonalService judicialPersonalService;

    private final BusinessJobExperienceService businessJobExperienceService;

    private final JobChangeExperienceService jobChangeExperienceService;
    private final JobExplainService jobExplainService;

    private final DictionaryRepository dictionaryRepository;

    private final JudicialTargetTypeSumCheckService judicialTargetTypeSumCheckService;

    private final CaseService caseService;

    @Value("${systemParameters.image.uploadPath}")
    private String uploadPath;

    @Value("${systemParameters.image.accessPrefix:/images/}")
    private String accessPrefix;

    public JudicialPersonalManagementController(JudicialPersonalService judicialPersonalService, BusinessJobExperienceService businessJobExperienceService, JobChangeExperienceService jobChangeExperienceService, JobExplainService jobExplainService, DictionaryRepository dictionaryRepository, JudicialTargetTypeSumCheckService judicialTargetTypeSumCheckService, CaseService caseService) {
        this.judicialPersonalService = judicialPersonalService;
        this.businessJobExperienceService = businessJobExperienceService;
        this.jobChangeExperienceService = jobChangeExperienceService;
        this.jobExplainService = jobExplainService;
        this.dictionaryRepository = dictionaryRepository;
        this.judicialTargetTypeSumCheckService = judicialTargetTypeSumCheckService;
        this.caseService = caseService;
    }

    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询司法人员列表 (分页)", notes = "可以作为 个人档案管理列表页面 查询列表")
    public ResultVo listByCondition(JudicialPersonalReqVo judicialPersonalReqVo, PageVo pageVo) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        Page<JudicialPersonalVo> page = judicialPersonalService.listByCondition(judicialPersonalReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }


    @ApiOperation(value = "业务岗位说明书 查询")
    @GetMapping("/findBusinessJobDetail/{id}")
    @ApiImplicitParam(name = "id", value = "个人档案Id", required = true, paramType = "path")
    public ResultVo findBusinessJobDetail(@PathVariable("id") Long personalId) {
        if (personalId == null) {
            throw new BizException(BizServiceEnum.NAME_IS_NULL);
        }
        JudicialPersonalDetailVo businessJobDetail = judicialPersonalService.findBusinessJobDetail(personalId);
        return ResultVoUtil.success(businessJobDetail);
    }

    @ApiOperation(value = "个人分值 （岗位说明书 中的 ） 查询")
    @GetMapping("/findSumCheckDetail")
    public ResultVo findSumCheckDetail(JudicialTargetTypeSumCheckJobExplainReqVo judicialTargetTypeSumCheckJobExplainReqVo) {
        JudicialPersonalFileVo judicialPersonalFileVo = new JudicialPersonalFileVo();
        //查询 汇总分数
        JudicialTargetTypeSumCheckVo judicialTargetTypeSumCheckVo = judicialTargetTypeSumCheckService.findSumCheckDetail(judicialTargetTypeSumCheckJobExplainReqVo);
        judicialPersonalFileVo.setJudicialTargetTypeSumCheckVo(judicialTargetTypeSumCheckVo);

        //查询 风险案件数量
        Long riskCaseCount = caseService.findCaseRiskCountByPersonalId(judicialTargetTypeSumCheckJobExplainReqVo.getPersonalId());

        //查询 不规范案件数量 TODO
        Long irregularCount = caseService.findCaseQualityFlowCountByPersonalId(judicialTargetTypeSumCheckJobExplainReqVo.getPersonalId());

        judicialPersonalFileVo.setRiskCaseCount(riskCaseCount);
        judicialPersonalFileVo.setIrregularCount(irregularCount);
        return ResultVoUtil.success(judicialPersonalFileVo);
    }


    @PostMapping("/imageUpload")
    @ApiOperation(value = "上传个人档案 头像图片")
    public ResultVo imageUpload(@RequestParam(value = "file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new BizException(BizServiceEnum.FILE_IS_NULL);
        }
        // 上传后的路径
        List<DictionaryEntity> dictionaryEntityList = dictionaryRepository.findByType(Constants.UPLOAD_PATH);
        if (CollectionUtils.isNotEmpty(dictionaryEntityList)) {
            DictionaryEntity dictionaryEntity = dictionaryEntityList.get(0);
            uploadPath = dictionaryEntity.getValue();
        }
        String accessPath = UploadUtils.uploadImage(file, uploadPath, accessPrefix);
        return ResultVoUtil.success(accessPath);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增个人档案信息")
    @BindCreateBy
    public ResultVo add(@RequestBody JudicialPersonalVo judicialPersonalVo) {
        judicialPersonalService.add(judicialPersonalVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改个人档案信息")
    @BindUpdateBy
    public ResultVo edit(@RequestBody JudicialPersonalVo judicialPersonalVo) {
        judicialPersonalService.edit(judicialPersonalVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除个人档案信息")
    @ApiImplicitParam(name = "id", value = "个人档案id", dataType = "Long", example = "1")
    public ResultVo delete(@PathVariable Long id) {
        judicialPersonalService.delete(id);
        return ResultVoUtil.success();
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除个人档案信息")
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "Long[]")
    public ResultVo deleteBatch(Long[] ids) {
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        judicialPersonalService.deletes(idList);
        return ResultVoUtil.success();
    }


    @PostMapping("/addBusinessJobExperience")
    @ApiOperation(value = "新增业务岗位经历")
    @BindCreateBy
    public ResultVo addBusinessJobExperience(@RequestBody BusinessJobExperienceReqVo businessJobExperienceReqVo) {
        businessJobExperienceService.add(businessJobExperienceReqVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/editBusinessJobExperience")
    @ApiOperation(value = "编辑业务岗位经历")
    @BindUpdateBy
    public ResultVo editBusinessJobExperience(@RequestBody BusinessJobExperienceReqVo businessJobExperienceReqVo) {
        businessJobExperienceService.edit(businessJobExperienceReqVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteBusinessJobExperience/{id}")
    @ApiOperation(value = "删除业务岗位经历")
    public ResultVo deleteBusinessJobExperience(@PathVariable Long id) {
        businessJobExperienceService.delete(id);
        return ResultVoUtil.success();
    }


    @PostMapping("/addJobChangeExperience")
    @ApiOperation(value = "新增检察官职位变更记录")
    @BindCreateBy
    public ResultVo addJobChangeExperience(@RequestBody JobChangeExperienceReqVo jobChangeExperienceReqVo) {
        jobChangeExperienceService.add(jobChangeExperienceReqVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/editJobChangeExperience")
    @ApiOperation(value = "编辑检察官职位变更记录")
    @BindUpdateBy
    public ResultVo editJobChangeExperience(@RequestBody JobChangeExperienceReqVo jobChangeExperienceReqVo) {
        jobChangeExperienceService.edit(jobChangeExperienceReqVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteJobChangeExperience/{id}")
    @ApiOperation(value = "删除检察官职位变更记录")
    public ResultVo deleteJobChangeExperience(@PathVariable Long id) {
        jobChangeExperienceService.delete(id);
        return ResultVoUtil.success();
    }


    @PostMapping("/addJobExplain")
    @ApiOperation(value = "新增岗位说明书")
    @BindCreateBy
    public ResultVo addJobExplain(@RequestBody JobExplainReqVo jobExplainReqVo) {
        jobExplainService.add(jobExplainReqVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/editJobExplain")
    @ApiOperation(value = "编辑岗位说明书")
    @BindUpdateBy
    public ResultVo editJobExplain(@RequestBody JobExplainReqVo jobExplainReqVo) {
        jobExplainService.edit(jobExplainReqVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteJobExplain/{id}")
    @ApiOperation(value = "删除岗位说明书")
    public ResultVo deleteJobExplain(@PathVariable Long id) {
        jobExplainService.delete(id);
        return ResultVoUtil.success();
    }


}